package com.scalar.chiptrack.sales;

import java.util.ArrayList;
import java.util.Iterator;

import com.scalar.chiptrack.users.User;
import com.scalar.chiptrack.commons.Holds;
import com.scalar.chiptrack.sales.dao.SalesDAO;
import com.scalar.ScalarException;

public class SalesOrderHold extends Holds {

  // ----------------------------------------------------- Instance Variables

  /**
   * Default Constructor
   *
   */
  public SalesOrderHold() {

  }

  public ArrayList load() throws ScalarException
  {    
      if(getCriteria() == Holds.SALES_ORDER_BY_CUSOTMER_PO) {
		  //System.out.println("First");
           // return SalesDAO.loadSalesOrders(getParameter("CustomerPO"));
			return SalesDAO.loadSalesOrdersInSalesHolds(getParameter("CustomerPO"), Integer.parseInt(getParameter("isSample")));
      } else if((getCriteria() == Holds.SALES_ORDER_BY_CUSTOMER) ||
            (getCriteria() == Holds.SALES_ORDER_BY_PRODUCT) ||(getCriteria() == Holds.SALES_ORDER_BY_CUSTOMER_AND_PRODUCT)||
            (getCriteria() == Holds.SALES_ORDER_BY_CUSTOMER_PRODUCT_YEAR)) {
				//System.out.println("Second");
                //return SalesDAO.loadSalesOrders(getParameter("ProductID"),getParameter("CustomerID"),getLoadHoldOrders()); 
				 return SalesDAO.loadSalesOrdersInSalesHolds(getParameter("ProductID"),getParameter("CustomerID"),getLoadHoldOrders(), getParameter("Year"), Integer.parseInt(getParameter("isSample")));
      } else if((getCriteria()==Holds.SALES_ORDER_BY_SALESORDERNO_PRODUCT_CUSTOMER)||((getCriteria()==Holds.SALES_ORDER_BY_SALESORDERNO_PRODUCT_CUSTOMER_YEAR))) {
            //System.out.println("Third");
            return SalesDAO.loadSalesOrdersInSalesHolds(getParameter("CustomerPO"),getParameter("ProductID"),getParameter("CustomerID"),getLoadHoldOrders(), getParameter("Year"), Integer.parseInt(getParameter("isSample")));
      }

      return null;
  }
  
  public void hold(User user) throws ScalarException 
  {
      if(getCriteria() == Holds.SALES_ORDER_BY_CUSOTMER_PO) {
          SalesDAO.holdSalesOrderLineItems(getParameter("CustomerPO"),true,user);
      } else if((getCriteria() == Holds.SALES_ORDER_BY_CUSTOMER) ||
            (getCriteria() == Holds.SALES_ORDER_BY_PRODUCT) ||
            (getCriteria() == Holds.SALES_ORDER_BY_CUSTOMER_AND_PRODUCT)) {
                SalesDAO.holdSalesOrderLineItems(getParameter("ProductID"),getParameter("CustomerID"),true,user);
      }
  }
  
  public void hold(Iterator list,User user) throws ScalarException
  { 
      SalesDAO.holdSalesOrderLineItems(list, user, true);
  }
  
  public void release(User user) throws ScalarException 
  {
      if(getCriteria() == Holds.SALES_ORDER_BY_CUSOTMER_PO) {
          SalesDAO.holdSalesOrderLineItems(getParameter("CustomerPO"),false,user);
      } else if((getCriteria() == Holds.SALES_ORDER_BY_CUSTOMER) ||
            (getCriteria() == Holds.SALES_ORDER_BY_PRODUCT) ||
            (getCriteria() == Holds.SALES_ORDER_BY_CUSTOMER_AND_PRODUCT)) {
                SalesDAO.holdSalesOrderLineItems(getParameter("ProductID"),getParameter("CustomerID"),false,user);
      }
  }
  
  public void release(Iterator list,User user) throws ScalarException
  {
      // System.out.println("In the release = " );
      SalesDAO.holdSalesOrderLineItems(list,user,false);
  }
  
  public void save(Iterator list, User user, String comments) throws ScalarException
  {
      SalesDAO.saveHoldsOnSalesOrderLineItems(list,user,comments);
  }
  
}